#include "drv_dog_foot.h"
/*
左前腿		PC8		TIM3   CH3  
右前腿		PC6		TIM3   CH1
左后腿		PC7     TIM3   CH2
右后腿		PC9     TIM3   CH4
0-180°是逆时针
*/
void drv_dog_foot_init(void)
{
	//PC8->TIM3_CH3 左前腿	PC9->TIM3_CH4 右后腿
	GPIO_InitTypeDef  GPIO_InitStruct;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseInitStruct;
	TIM_OCInitTypeDef  TIM_OCInitStruct;
	

	RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC,ENABLE);
	RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE);
		
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
	GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_6|GPIO_Pin_7;
	GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStruct);

	GPIO_PinAFConfig(GPIOC,GPIO_PinSource8,GPIO_AF_TIM3);
	GPIO_PinAFConfig(GPIOC,GPIO_PinSource9,GPIO_AF_TIM3);
	GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_TIM3);
	GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_TIM3);

	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period=20000-1;
	TIM_TimeBaseInitStruct.TIM_Prescaler=84-1;
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);

	TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1;
	TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;
	TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;
	TIM_OCInitStruct.TIM_Pulse=500-1;
	TIM_OC3Init(TIM3,&TIM_OCInitStruct);
	TIM_OC4Init(TIM3,&TIM_OCInitStruct);
	TIM_OC1Init(TIM3,&TIM_OCInitStruct);
	TIM_OC2Init(TIM3,&TIM_OCInitStruct);

	TIM_ARRPreloadConfig(TIM3, ENABLE);
	TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
	TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
	TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
	TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
	TIM_CtrlPWMOutputs(TIM3, ENABLE);

	TIM_Cmd(TIM3,ENABLE);
	
	//PA2->TIM2_CH3	右前腿		PA3->TIM2_CH4 左后腿
	
}
void drv_dog_stand(void)//起立
{
	TIM_SetCompare3(TIM3, 1500-1);//左前
	TIM_SetCompare3(TIM3, 1500-1);//右前
	TIM_SetCompare4(TIM3, 1500-1);//左后
	TIM_SetCompare4(TIM3, 1500-1);//右后
}
void drv_dog_lie(void)//躺下
{
	TIM_SetCompare3(TIM3, 500-1);//左前
	TIM_SetCompare3(TIM3, 500-1);//右前
	TIM_SetCompare4(TIM3, 500-1);//左后
	TIM_SetCompare4(TIM3, 500-1);//右后
}
void drv_dog_run(void)
{
		TIM_SetCompare3(TIM3, 1000-1);//左前45
		TIM_SetCompare1(TIM3, 2000-1);//右前45(逆时针135)
		TIM_SetCompare2(TIM3, 2000-1);//左后135
		TIM_SetCompare4(TIM3, 1000-1);//右后135（逆时针45）
		drv_systick_ms(1000);
	while(drv_dog_stop==0)
	{
		TIM_SetCompare3(TIM3, 2000-1);//左前
		TIM_SetCompare4(TIM3, 2000-1);//右后
			
		TIM_SetCompare1(TIM3, 1000-1);//右前
		TIM_SetCompare2(TIM3, 1000-1);//左后
		drv_systick_ms(500);
		TIM_SetCompare3(TIM3, 1000-1);//左前
		TIM_SetCompare3(TIM3, 2000-1);//右前
			
		TIM_SetCompare1(TIM2, 2000-1);//左后
		TIM_SetCompare2(TIM3, 1000-1);//右后
		drv_systick_ms(500);
	}
	drv_dog_stand();
}
